import {height, width} from '../config'
import {setImageFromBase64} from "../setImage";
import dayjs from "dayjs";
import {dida365} from "../dida365";
import {NodeCanvas} from "../NodeCanvas";

const updateDot = async () => {

    const currentDate = dayjs()
    const nodeCanvas = new NodeCanvas(width, height)
        .drawText(`${currentDate.format('YYYY')}年${currentDate.format('MM')}月`, Math.round(width * 0.77), 20, {
            font: "20px ddjinbu",
            textAlign: "center",
            textBaseline: "middle"
        })
        .drawText(`${currentDate.format('DD')}`, Math.round(width * 0.77), height / 2, {
            font: "50px ddjinbu",
            textAlign: "center",
            textBaseline: "middle"
        })
        .drawText(`星期${"日一二三四五六".charAt(Number(currentDate.format('d')))} ${currentDate.format("HH:mm")}`, Math.round(width * 0.77), height - 4, {
            font: "20px ddjinbu",
            textAlign: "center",
            textBaseline: "bottom"
        })
        .strokeRect(2, 2, width - 4, height - 4, 2)
        .stroke(Math.round(width * 0.55), 2, Math.round(width * 0.55), height - 4)

    const dida365Data = await dida365()
    const update = dida365Data.syncTaskBean.update.map(i => {
        return {
            ...i,
            startTime: dayjs(i.startDate).valueOf(),
            startDate: dayjs(i.startDate).format('MM-DD'),
            dueDate: dayjs(i.startDate).format('MM-DD'),
        }
    })
    update.sort((a, b) => a.startTime - b.startTime)

    const length = 4
    for (let i = 0; i < length; i++) {
        const item = update[i]
        const itemHeight = (height - 2 - 2) / 5
        if (item) {
            nodeCanvas.drawText(item.title, 10, 2 + itemHeight * i + itemHeight / 2, {
                font: "20px ddjinbu",
                textAlign: "left",
                textBaseline: "middle"
            })
        }
        // 不是最后一个
        if (i !== length - 1) {

        }
        nodeCanvas.strokeDash(2, 2 + itemHeight * (i + 1), Math.round(width * 0.55), 2 + itemHeight * (i + 1))
    }
    const itemHeight = (height - 2 - 2) / 5

    // 末次月经
    const lastData = dayjs('2025/07/10');

// 今天
    const today = dayjs();

// 计算已过天数
    const diffDays = today.diff(lastData, 'day'); // 天数差

// 已过孕周
    const weeksPassed = Math.floor(diffDays / 7);
    const daysPassed = diffDays % 7;
    nodeCanvas.drawText(`进度${weeksPassed}+${daysPassed}≈${Math.floor(diffDays/(40*7)*100)}%`, 10, 2 + itemHeight * 4 + itemHeight / 2, {
        font: "20px ddjinbu",
        textAlign: "left",
        textBaseline: "middle"
    })
    setImageFromBase64(nodeCanvas.toBase64())
}


updateDot()
